<template>
  <h1>员工列表练习</h1>
  <!-- 1.准备3个输入框,进行双向绑定 -->
  <input type="text" placeholder="请输入员工姓名" v-model="e.name">
  <input type="text" placeholder="请输入员工工资" v-model="e.salary">
  <input type="text" placeholder="请输入员工岗位" v-model="e.job">
  <hr>
  <!-- 2.准备按钮,并绑定点击事件 -->
  <button @click="add()">点我添加</button>
  <hr>
  <table border="1px">
    <tr>
      <th>编号</th> <th>员工姓名</th> <th>员工工资</th> <th>员工岗位</th>
    </tr>
    <tr v-for="(emp,i) in arr">
      <td>{{i+1}}</td>
      <td>{{emp.name}}</td>
      <td>{{emp.salary}}</td>
      <td>{{emp.job}}</td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";
//1.准备数组用来保存多个员工对象
const arr = ref([]);
//2.准备一个空的员工模板对象
const e = ref({name:'',salary:'',job:''});
//3.定义添加员工方法,点击按钮时触发
const add = ()=>{
  //4.准备一个新的员工对象,将用户在输入框输入的值存入新对象对应的属性中
  let newEmp = {
    name:e.value.name,
    salary:e.value.salary,
    job:e.value.job
  }
  //5.将准备好的新员工对象存入员工数组中
  arr.value.push(newEmp);
  //6.将输入框的值清空
  e.value = {name:'',salary:'',job:''};
}
</script>

<style scoped>

</style>